<? 
	include('base.php');
	User::protect();
	include_class('releases');
	include_class('release_tracks');
	include_class('m2');
	$section = 'releases';
	$rt = ReleaseTrack::get($_GET['id']);
	
	if (!db::isError($rt)) {
		$r = $rt->getReleaseObject();
		$ma = $rt->getAVAreaObject();	
	
		switch($_GET['task']) {
			case 'register_media':
				$res = $rt->registerMedia();				
				if (!db::isError($res)) {
					header('Location: release_track_media.php?id=' . $_GET['id']);
				}
				break;
			case 'rescan':
				if (!db::isError($ma)) {
					$res = $ma->rescan($rt);
					if (!db::isError($res)) {
						header('Location: release_track_media.php?id=' . $_GET['id']);
					}
				}
				break;
			case 'move_up':
				if (!db::isError($ma)) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$res = $mi->moveUp($rt);
						if (!db::isError($res)) {
							header('Location: release_track_media.php?id=' . $_GET['id']);
						}
					} else {
						$res = $mi;
					}
				}
				break;
			case 'move_down':
				if (!db::isError($ma)) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$res = $mi->moveDown($rt);
						if (!db::isError($res)) {
							header('Location: release_track_media.php?id=' . $_GET['id']);
						}
					} else {
						$res = $mi;
					}
				}
				break;
		}
	}
	
	$page_title = 'Release Track Media';
	include('layout/header.php');



if (!db::isError($rt) && !db::isError($r)) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	<a href="release_edit.php?id=<?=$r->getID()?>">View/Edit Release &#62;</a>
	<a href="release_track_edit.php?id=<?=$rt->getID()?>">View/Edit Release Track &#62;</a>
	Media &#62;
</div>

<?	
	if ($res) { // it's an error
		if (db::isError($res)) {
			$res->outputList();
		}
	}
	
	if (is_object($ma) && !db::isError($ma)) {
	
		$exitURL = "release_track_edit.php?id=" . $_GET['id'];
		$canEdit = User::isAdmin();
		$exitText = "Return to Track";
		if ($canEdit) {
			$addURL = "release_track_media_add.php?id=" . $_GET['id'];
		}
		$editURL = "release_track_media_edit.php?id={$_GET['id']}&";
		include('layout/media_list.php');
	} else if (User::isAdmin()) {	
		$ma->outputDialog('Register Track', $PHP_SELF . '?task=register_media&amp;id=' . $_GET['id']);
	} else {
		$ma->outputDialog('Return to Track','release_track_edit.php?id=' . $_GET['id']);
	 } ?>


<? } else { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	Invalid Entry &#62;
</div>

<?=$to->outputDialog('Return to Releases','releases.php', 'Please specify a valid release track ID.');?>

<? } ?>

<? include('layout/footer.php'); ?>